//
//  ZYCProtalCalendarViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/18.
//

#import "ZYCProtalCalendarViewController.h"
#import "ZYCProtalCalendarCell.h"
#import "ZYCNoticeCalendarDetailContentViewController.h"
#import "ZYCNoticeCalendarModel.h"
#import "ZYCNoDataView.h"

static NSInteger pageSize = 10;
@interface ZYCProtalCalendarViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, strong) ZYCNoDataView *noDataView;
@end

@implementation ZYCProtalCalendarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"周历";
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadNewData];

}
- (void)loadNewData{
    self.pageNum = 1;
    NSDictionary *params = @{
        @"pageNum":@(self.pageNum),
        @"pageSize":@(pageSize),
        @"noticeType":@"3"
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_NoticeCalendarList requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCNoticeCalendarModel class] json:responseObject[@"body"][@"list"]];
                [self.dataSource removeAllObjects];
                [self.dataSource addObjectsFromArray:array];
                [self.tableView reloadData];
                
                [self reset];
                [self.tableView.mj_header endRefreshing];
                if (self.dataSource.count == 0) {
                    self.noDataView.hidden = NO;
                    self.tableView.hidden = YES;
                }else{
                    self.noDataView.hidden = YES;
                    self.tableView.hidden = NO;
                }
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }

        } failure:^(NSError * _Nonnull error) {
            [self.tableView.mj_header endRefreshing];
        }];
}


-(void)loadMoreData{
    self.pageNum ++;
    NSDictionary *params = @{
        @"pageNum":@(self.pageNum),
        @"pageSize":@(pageSize),
        @"noticeType":@"3"
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_NoticeCalendarList requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCNoticeCalendarModel class] json:responseObject[@"body"][@"list"]];
                if (array.count == 0) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.dataSource addObjectsFromArray:array];
                    [self.tableView reloadData];
                    [self.tableView.mj_footer endRefreshing];
                }
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }

        } failure:^(NSError * _Nonnull error) {
            [self.tableView.mj_footer endRefreshing];
        }];
}


- (void)reset{
    [self.tableView.mj_footer setRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.tableView.mj_footer resetNoMoreData];
}

- (void)loadUpdateReadWithCell:(ZYCProtalCalendarCell *)cell indexPath:(NSIndexPath *)indexPath{
    ZYCNoticeCalendarModel *model  = cell.cModel;
    NSDictionary *params = @{
        @"noticeId":model.noticeId
    };
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_UpdateReadFlag requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            if (SUCCESS(responseObject[@"code"])) {
                cell.cModel.readFlag = @"1";
                [MrjSVProgressHUD showSuccessWithStatus:responseObject[@"body"]];
                NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:indexPath.section];
                [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCWeakSelf
    ZYCProtalCalendarCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCProtalCalendarCell class]) forIndexPath:indexPath];
    [cell setSectionCircularTableView:tableView withIndexPath:indexPath];
    cell.cModel = self.dataSource[indexPath.section];
    cell.clickReadBlock = ^(ZYCProtalCalendarCell * _Nonnull cell) {
        [weakSelf loadUpdateReadWithCell:cell indexPath:indexPath];
    };
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 105.0;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ZYCNoticeCalendarModel *model = self.dataSource[indexPath.section];
    ZYCNoticeCalendarDetailContentViewController *vc = [ZYCNoticeCalendarDetailContentViewController new];
    vc.noticeId = model.noticeId;
    vc.type = ZYCNoticeCalendarDetailContentTypeRcv;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCProtalCalendarCell class] forCellReuseIdentifier:NSStringFromClass([ZYCProtalCalendarCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}

- (ZYCNoDataView *)noDataView {
    if (!_noDataView) {
        _noDataView = [[ZYCNoDataView alloc] initWithFrame:self.tableView.frame];
        _noDataView.hidden = YES;
        [self.view addSubview:_noDataView];
    }
    return _noDataView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
